<?php

namespace Emoney\EpayBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Emoney\EpayBundle\Entity\Retreives;
use Emoney\EpayBundle\Form\Type\RetreivesType;
use Symfony\Component\HttpFoundation\Request;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PayementController
 *
 * @author FFOZEU
 */
class RetreivesController extends Controller{
    //put your code here
    function newAction(Request $request) {
        $retrieve = new Retreives();
        $form = $this->createForm(new retreivesType(), $retrieve);
        
        if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);
			$user = $this->container->get('security.context')->getToken()->getUser();

			if (!is_object($user) ) {
			    $this->get('session')->setFlash('notice', 'This user does not have access to this section.');
				return $this->render('EmoneyEpayBundle::error.html.twig', array());
			}
             $retrieve->setUserId($user->getId());
			 $dated = date('Y-m-d');
			 $retrieve->setAskRdate(new \DateTime($dated));
			 $retrieve->setState(false);
            if ($form->isValid()) {
                // perform some action, such as saving the task to the database
				$em = $this->container->get('doctrine')->getEntityManager();
				$em->persist($retrieve);
				$em->flush();
                return $this->redirect($this->generateUrl('EmoneyEpayBundle_listrereives'));
            }
        }else{
            return $this->render('EmoneyEpayBundle:retreives:create.html.twig', array(
                'form' => $form->createView(),
            ));
        }
		 return $this->render('EmoneyEpayBundle:retreives:create.html.twig', array(
                'form' => $form->createView(),
            ));
    }
	
    //list all retreives actions for a user
    function listAction(Request $request) {

        $user = $this->container->get('security.context')->getToken()->getUser();
		if (!is_object($user) ) {
                $this->get('session')->setFlash('notice', 'This user does not have access to this section.');
				return $this->render('EmoneyEpayBundle::error.html.twig', array());
		}
        $retrieves = new Retreives();
        $form = $this->createForm(new RetreivesType(), $retrieves);

		$em = $this->container->get('doctrine')->getEntityManager();
		$retreives = $em->getRepository('EmoneyEpayBundle:Retreives')->findByUserId($user->getId());
		return $this->container->get('templating')->renderResponse('EmoneyEpayBundle:Retreives:list.html.twig',array(
		'retreives' => $retreives,
		'form' => $form->createView()));
    }
	
    //oder for a retrieve
    function orderAction(Request $request) {
        $retreive = new Retreives();
        $form = $this->createForm(new SitenameType(), $sitename);
        
        if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);
			$user = $this->container->get('security.context')->getToken()->getUser();

			if (!is_object($user) ) {
			    $this->get('session')->setFlash('notice', 'This user does not have access to this section.!');
				return $this->render('EmoneyEpayBundle::error.html.twig', array());
			}
             $retreive->setUserId($user);
            if ($form->isValid()) {
                // perform some action, such as saving the task to the database
				$em = $this->container->get('doctrine')->getEntityManager();
				$em->persist($retreive);
				$em->flush();
                return $this->render('EmoneyEpayBundle::info.html.twig', array( ));
            }
        }else{
            return $this->render('EmoneyEpayBundle::info.html.twig', array( ));
        }
    }
}

?>
